package top.italks.manufactureproject.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.HashMap;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import top.italks.manufactureproject.R;
import top.italks.manufactureproject.bean.UpdateLightSwitch;
import top.italks.manufactureproject.bean.UserWorkEnvironmentalGetInfo;
import top.italks.manufactureproject.constant.Config;
import top.italks.manufactureproject.utils.HttpUtils;
import top.italks.manufactureproject.utils.RetrofitService;

//工厂灯光
public class EnvironmentActivity extends AppCompatActivity {

    @BindView(R.id.lightSwitch)
    TextView lightSwitch;

    @BindView(R.id.open)
    Button open;
    @BindView(R.id.close)
    Button close;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_environment);
        ButterKnife.bind(this);
        getInfo();
    }

    @OnClick({R.id.open, R.id.close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.open:
                updateLightSwitch(1);
                break;
            case R.id.close:
                updateLightSwitch(0);
                break;
        }
    }

    private void updateLightSwitch(final int light) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", 1);
        map.put("lightSwitch", light);
        HttpUtils.post(
            Config.url + "/dataInterface/UserWorkEnvironmental/updateLightSwitch"
            , map
            , new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String json = response.body().string();
                    final UpdateLightSwitch bean = new Gson()
                        .fromJson(json, UpdateLightSwitch.class);
                    if (bean.getStatus() == 200) {
//                        runOnUiThread(new Runnable() {
//                            @Override
//                            public void run() {
//                                if(light==0){
//                                    lightSwitch.setText("关灯状态");
//                                    close.setEnabled(false);
//                                    open.setEnabled(true);
//                                }else {
//                                    lightSwitch.setText("开灯状态");
//                                    close.setEnabled(true);
//                                    open.setEnabled(false);
//                                }
//                            }
//                        });

//                        getInfo();

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (bean.getData() != null) {
                                    if (bean.getData().getLightSwitch().equals("0")) {
                                        lightSwitch.setText("关灯状态");
                                        close.setEnabled(false);
                                        open.setEnabled(true);
                                    } else {
                                        lightSwitch.setText("开灯状态");
                                        close.setEnabled(true);
                                        open.setEnabled(false);
                                    }
                                }
                            }
                        });

                    }
                }
            });


    }

    private void getInfo() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", Config.userWorkId);
//        HttpUtils.post(Config.url + "/dataInterface/UserWorkEnvironmental/getInfo"
//                , map
//                , new Callback() {
//                @Override
//                public void onFailure(Call call, IOException e) {
//                    e.printStackTrace();
//                }
//
//                @Override
//                public void onResponse(Call call, Response response) throws IOException {
//                    String json = response.body().string();
//                    final UserWorkEnvironmentalGetInfo info = new Gson()
//                        .fromJson(json, UserWorkEnvironmentalGetInfo.class);
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            if (info.getData().get(0).getLightSwitch() == 0) {
//                                lightSwitch.setText("关灯状态");
//                                close.setEnabled(false);
//                                open.setEnabled(true);
//                            } else {
//                                lightSwitch.setText("开灯状态");
//                                close.setEnabled(true);
//                                open.setEnabled(false);
//                            }
//                        }
//                    });
//                }
//            });

        RetrofitService.getApiService().userWorkEnvironmentalGetInfo(map)
            .enqueue(new retrofit2.Callback<UserWorkEnvironmentalGetInfo>() {
                @Override
                public void onResponse(retrofit2.Call<UserWorkEnvironmentalGetInfo> call,
                    retrofit2.Response<UserWorkEnvironmentalGetInfo> response) {
                    final UserWorkEnvironmentalGetInfo info = response.body();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (info.getData().get(0).getLightSwitch() == 0) {
                                lightSwitch.setText("关灯状态");
                                close.setEnabled(false);
                                open.setEnabled(true);
                            } else {
                                lightSwitch.setText("开灯状态");
                                close.setEnabled(true);
                                open.setEnabled(false);
                            }
                        }
                    });
                }

                @Override
                public void onFailure(retrofit2.Call<UserWorkEnvironmentalGetInfo> call,
                    Throwable t) {

                }
            });
    }
}
